package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.server.mapper.QuestionMapper;
import com.tanhua.server.pojo.Question;
import com.tanhua.server.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;


    public Question queryQuestion(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        return this.questionMapper.selectOne(queryWrapper);
    }

    public int saveQuertionById(Question question) {
        Question queryQuestion = queryQuestion(UserThreadLocal.get().getId());
        if (queryQuestion == null) {
            return questionMapper.insert(question);
        }
        queryQuestion.setTxt(question.getTxt());
        return questionMapper.updateById(queryQuestion);
    }
}